package com.example.sxproject.mapper;

import com.example.sxproject.pojo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CardMapper {
    @Select("SELECT * FROM card WHERE userId = #{userId}")
    List<Card> findByUserId(Integer userId);

    //添加insert：添加卡
    @Insert("insert into card" +
            "(id, idNumber, cardType, password, balance, statusId,userId,beginDate,repaid,total,rate,repayDate)" +
            " values (#{id}, #{idNumber}, #{cardType}, #{password}, #{balance}, #{statusId}," +
            "#{userId},#{beginDate},#{repaid},#{total},#{rate},#{repayDate})")
    void addCard(Card card);

    //查询：查询银行卡信息
    @Select("select * from card where idNumber=#{idNumber}")
    Card findByIdNumber(String idNumber);

    //更新：更改指定银行卡的密码
    @Update("UPDATE card SET password=#{password} WHERE idNumber=#{idNumber}")
    void updatePassword(String idNumber, String password);

    //更新：更新全部
    @Update("UPDATE card SET " +
            "idNumber = #{idNumber}, " +
            "cardType = #{cardType}, " +
            "password = #{password}, " +
            "balance = #{balance}, " +
            "statusId = #{statusId}, " +
            "userId = #{userId}, " +
            "beginDate = #{beginDate}, " +
            "repaid = #{repaid}, " +
            "total = #{total}, " +
            "rate = #{rate}, " +
            "repayDate = #{repayDate} " +
            "WHERE id = #{id}")
    void updateALL(Card card);

    //更新：更改指定银行卡的余额,
    @Update("UPDATE card SET balance=#{balance} WHERE idNumber=#{idNumber}")
    void updateBalance(String idNumber, double balance);

    //更新：实现假删除
    @Update("UPDATE card SET statusId=#{statusId} WHERE idNumber=#{idNumber}")
    void deleteCard(String idNumber, Integer statusId);

    //根据卡号修改所属用户
    @Update("update card set userId=#{userId} where idNumber=#{idNumber}")
    void updateStatusId(String idNumber,Integer userId);

    //查待审核银行卡开户申请
    @Select("select * from card where statusId='4'")
    List<Card> findByStatusId();

    //根据银行卡类型和所属用户查询银行卡信息
    @Select({
            "<script>",
            "select *",
            "from card",

            "<where>",
            "<if test='cardType!=null'>",
            "cardType=#{cardType} ",
            "</if>",

            "<if test='userId!=null'>",
            "AND userId =#{userId}",
            "</if>",

            "</where>",
            "</script>"
    })
    List<Card> selectByTI(Card card);


    @Update("UPDATE card SET " +
            "idNumber = #{idNumber}, " +
            "statusId = '1', " +
            "beginDate = #{beginDate}, " +
            "total = #{total} " +
            "WHERE id = #{id}")
    void kaihu(Card card);

    //更新：实现驳回
    @Update("UPDATE card SET statusId='5' WHERE id=#{id}")
    void bohui(Integer id);

}